package com.okcoin.dao.impl;

import com.google.gson.Gson;
import com.okcoin.common.MongoConst;
import com.okcoin.common.OKConst;
import com.okcoin.dao.DataWriteDao;
import com.okcoin.entity.Futureusd;
import com.okcoin.entity.FutureusdIndex;
import com.okcoin.entity.FutureusdKLine;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.data.mongodb.core.MongoTemplate;


public class DataWriteDaoImp implements DataWriteDao {

    @Override
    public  void webscoketWrite(JSONObject JO, String collectionName) {
        Gson gson = new Gson();
        //指数系列
        if(OKConst.FUTUREUSD_INDEX_LIST.contains(collectionName)){
            MongoConst.MONGO_TEMPLATE.save(gson.fromJson( JO.getJSONObject("data").toString(), FutureusdIndex.class),collectionName);
        }

        //当周、次周、季度合约
        if(OKConst.FUTUREUSD_LIST.contains(collectionName)){
            Futureusd futureusd = gson.fromJson( JO.getJSONObject("data").toString(), Futureusd.class);
            futureusd.setTimestamp(Long.toString(System.currentTimeMillis()));
            MongoConst.MONGO_TEMPLATE.save(futureusd,collectionName);
        }

        //K线数据
        if(OKConst.FUTUREEUSD_K_LINE_LIST.contains(collectionName)){
            JSONArray jsonArray = (JSONArray) JO.getJSONArray("data").get(0);
            if(jsonArray.length()==7){
                FutureusdKLine futureusdKLine = new FutureusdKLine();
                futureusdKLine.setTimestamp(jsonArray.get(0).toString());
                futureusdKLine.setStart(jsonArray.get(1).toString());
                futureusdKLine.setHighest(jsonArray.get(2).toString());
                futureusdKLine.setLowest(jsonArray.get(3).toString());
                futureusdKLine.setEnd(jsonArray.get(4).toString());
                futureusdKLine.setTransactionNum(jsonArray.get(5).toString());
                futureusdKLine.setTransactionBNum(jsonArray.get(6).toString());
                MongoConst.MONGO_TEMPLATE.save(futureusdKLine,collectionName);
            }
        }

    }
}
